package main

import (
	"fmt"
	"github.com/antchfx/htmlquery"
	"io"
	"net/http"
)

func main() {

	var client = http.Client{}
	baseURL := "https://movie.douban.com/top250?start=%d&filter="

	for start := 0; start < 250; start += 25 {
		url := fmt.Sprintf(baseURL, start)
		//fmt.Println(url)

		req, err := http.NewRequest("GET", url, nil)
		if err != nil {
			panic(err)
		}
		req.Header.Set("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36")

		resp, err := client.Do(req)
		if err != nil {
			panic(err)
		}
		defer func(Body io.ReadCloser) {
			err := Body.Close()
			if err != nil {
				panic(err)
			}
		}(resp.Body)

		doc, err := htmlquery.Parse(resp.Body)
		if err != nil {
			panic(err)
		} //*[@id="content"]/div/div[1]/ol/li[1]/div/div[2]/div[2]/div/span[1]
		ols := htmlquery.Find(doc, "//*[@id=\"content\"]/div/div[1]/ol/li")

		for _, ol := range ols {
			name1 := htmlquery.FindOne(ol, "//div/div[2]/div[2]/div/span[1]/@class")
			name2 := htmlquery.FindOne(ol, "//div/div[2]/div[2]/div/span[2]")
			name3 := htmlquery.FindOne(ol, "//div/div[2]/div[2]/div/span[4]")

			var text1, text2, text3 string

			if name1 != nil {
				text1 = htmlquery.InnerText(name1)
			}

			if name2 != nil {
				text2 = htmlquery.InnerText(name2)
			}

			if name3 != nil {
				text3 = htmlquery.InnerText(name3)
			}

			name4 := text1 + " " + text2 + " " + text3
			fmt.Println(name4)
		}
	}

}
